<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class login extends CI_Controller {
	var $data = array();

	public function __construct() {
		parent::__construct();

		$this -> output -> cache(0);
		$this -> data['current_module'] = site_url("login");
		$this -> data['css_url'] = base_url() . 'css/';
		$this -> data['javscript_url'] = base_url() . 'script/';
	//	$this->session->sess_destroy();
	}
	
	public function logout(){
		$this->session->sess_destroy();
		$this -> session -> unset_userdata('authen');
		$this -> session -> unset_userdata('role_id');
		
		
		redirect('login', 'location', 301);
	}
	

	public function index() {
		if ($this -> session -> userdata('authen') == TRUE) {
			if($this -> session -> userdata('role_id')==4){
				redirect('truck/upload', 'location', 301);
			}else{
				redirect('manager/dashboard', 'location', 301);
			}
		} else {
			$this -> load -> view('login', $this -> data);
		}
	}
	public function index2() {
		$this -> load -> view('login2', $this -> data);
	}

	public function authen() {
		$sql = "SELECT `UUID`,`UID`,`Username`,users.`Name` as FirstName,`Lastname`,role.name as rolename,users.`roleid` FROM `users` inner join role on(users.`roleid` =role.`roleid`) where username=? and password=?";
		$query = $this -> db -> query($sql, array($_REQUEST['username'], $_REQUEST['password']));
		if ($query -> num_rows()) {
			$row = $query -> row();

			$newdata = array('role_id' => $row -> roleid, 'FirstName' => $row -> FirstName, 'Lastname' => $row -> Lastname, 'rolename' => $row -> rolename, 'authen' => TRUE);

			$this -> session -> set_userdata($newdata);
			if ($row -> roleid == 4) {
				redirect('truck/upload', 'location', 301);
			} else {
				redirect('manager/dashboard', 'location', 301);
			}
		} else {
			$this -> session -> set_userdata(array('authen' => FALSE));
			echo "<script>alert('กรูณาตรวจสอบ User password');window.location ='".site_url("login")."';</script>";
		}

	}

}
?>